package com.ljyh.cygnus.screens.viewmodel

import com.ljyh.cygnus.model.CommandUiState
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow

class APNViewModel: BaseViewModel() {
    private val _uiState = MutableStateFlow<CommandUiState>(CommandUiState.Idle)
    val uiState: StateFlow<CommandUiState> = _uiState.asStateFlow()

    private var sqlitePath=""
    private val dbPath = "/data/user_de/0/com.android.providers.telephony/databases/telephony.db"

    fun setSqlitePath(path:String){
        sqlitePath=path
    }
    fun disableAPN() {

        if (sqlitePath == null) {
            // 处理错误，例如更新UI状态
            _uiState.value = CommandUiState.Error("sqlite3 binary not found!", null)
            return
        }
        executeRootCommands(
            _uiState,
            // 使用我们自己的sqlite3的绝对路径
            "$sqlitePath $dbPath \"UPDATE carriers SET name = name || '##' || numeric WHERE numeric != '00000';\"",
            "$sqlitePath $dbPath \"UPDATE carriers SET numeric = '00000';\""
        )
    }

    fun enableAPN() { // 这是恢复
        if (sqlitePath == null) {
            _uiState.value = CommandUiState.Error("sqlite3 binary not found!", null)
            return
        }
        executeRootCommands(
            _uiState,
            "$sqlitePath $dbPath \"UPDATE carriers SET numeric = substr(name, instr(name, '##') + 2), name = substr(name, 1, instr(name, '##') - 1) WHERE name LIKE '%##%';\""
        )
    }

    fun restrictionAPN(){
        executeRootCommands(
            _uiState,
            "chmod 444 $dbPath"
        )
    }
}